<template>
  <div>
    <el-dialog :visible.sync='isShow' title='添加交易组' width="800px" @open="openDialogInit" @close="resetForm" :close-on-click-modal=false>
      <div v-loading="loading">
        <el-card>
          <el-form :model="formInline" ref="formInline" size="mini" :rules="rules" label-position="left" label-width="125px">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label='应用编号' prop='AppNum'>
                  <el-select v-model="formInline.AppNum" @change="handleTranGroup" placeholder="请选择应用编号">
                    <el-option v-for="item in appQueryList" :key="item.devState" :label="item.AppName" :value="item.AppNum">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="交易组编号:" prop="TranGroupNum">
                  <el-input v-model="formInline.TranGroupNum" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="交易组名称:" prop="TranGroupName">
                  <el-input v-model="formInline.TranGroupName" placeholder="请输入交易组名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label='交易组分类' prop='TranGroupClass'>
                  <el-select v-model="formInline.TranGroupClass" placeholder="请选择交易组分类">
                    <el-option v-for="item in TranGroupClass" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="法人号:" prop="LegalPersonNum">
                  <el-input v-model="formInline.LegalPersonNum" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人名称:" prop="LegalPersonName">
                  <el-input v-model="formInline.LegalPersonName" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="生效日期:" prop="EffeDate">
                  <el-date-picker v-model="formInline.EffeDate" value-format="yyyyMMdd" type="date" placeholder="选择日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="失效日期:" prop="ExpiDate">
                  <el-date-picker v-model="formInline.ExpiDate" value-format="yyyyMMdd" type="date" placeholder="选择日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label='交易组状态' prop='TranGroupStatus'>
                  <el-select v-model="formInline.TranGroupStatus" placeholder="请选择交易组分类">
                    <el-option v-for="item in TranGroupStatus" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="排序号:" prop="SortNum">
                  <el-input-number v-model="formInline.SortNum" :min="1" :max="10" label="描述文字"></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-row style="margin:20px 0;text-align:right">
          <el-button size="mini" @click="isShow=false">取消</el-button>
          <el-button type="primary" size="mini" @click="addTranGroup">确认</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/common/utils/request";
import appQuery from "@/message/shared/appquery"; // 应用编号查询
import toolBox from "@/common/utils/toolBox";
import tranGroup from "@/message/branchManage/tranGroupManage/tranGroup";
import tranGroupNum from "@/message/branchManage/tranGroupManage/tranGroupNum";
export default {
  props: { createBrand: Object },
  data() {
    let startdateCheck = (rule, value, callback) => { //自定义表单验证 校验IP地址
        if (value != "") {
          let time = toolBox.getNowTime(0);
          if (value - time >= 0) {
            callback();
          } else {
            callback(new Error("生效日期必须大于当前日期"));
          }
        } else {
          callback();
        }
      };
      let enddateCheck = (rule, value, callback) => { //自定义表单验证 校验IP地址
        if (value != "") {
          if (value - this.formInline.EffeDate >= 0) {
            callback();
          } else {
            callback(new Error("失效日期必须大于生效日期"));
          }
        } else {
          callback();
        }
      };
    return {
      loading: false,
      appQueryList: [],
      formInline: {},
      AppNum: "",
      TranGroupStatus: [
        {
          value: "0",
          label: "0-停用"
        },
        {
          value: "1",
          label: "1-启用"
        }
      ],
      TranGroupClass: [
        {
          value: "01",
          label: "01-机构组"
        },
        {
          value: "02",
          label: "02-设备应用"
        }
      ],
      rules: {
        AppNum: [
          { required: true, message: "请选择应用编号", trigger: "change" }
        ],
        TranGroupName: [
          { required: true, message: "请输入交易组名称", trigger: "blur" }
        ],
        EffeDate: [
          { type: 'string', required: true, message: '请选择日期', trigger: 'change' },{validator: startdateCheck,trigger: 'blur' }
        ],
        ExpiDate: [
          { type: 'string', required: true, message: '请选择日期', trigger: 'change' },{validator: enddateCheck,trigger: 'blur' }
        ],
        TranGroupClass: [
          { required: true, message: "请选择应用分类", trigger: "change" }
        ],
        TranGroupStatus: [
          { required: true, message: "请选择应用组状态", trigger: "change" }
        ],
        SortNum: [
          { required: true, message: "请输入排序号", trigger: "blur" }
        ],
      }
    };
  },
  computed: {
    isShow: {
      get: function() {
        return this.createBrand.isShow;
      },
      set: function(newValue) {
        this.createBrand.isShow = newValue;
      }
    }
  },
  methods: {
    openDialogInit() {
      if (this.$refs.formInline) {
        this.$refs["formInline"].clearValidate(); //清楚表单验证
        this.$refs["formInline"].resetFields(); //清除表单验证
      }
      this.formInline.LegalPersonNum = this.$store.getters.LegalPersonNum;
      this.formInline.LegalPersonName = this.$store.getters.LegalPersonName;
      this.appQueryInit();
    },
    // 设备型号下拉框初始化查询
    appQueryInit() {
      let resBody = new appQuery();
      resBody.data.UserNum = this.$store.getters.UserNum;
      request(resBody)
        .then(response => {
          this.appQueryList = response.RspInfo.Result;
        })
        .catch(error => {
          console.error("查询错误", error);
        });
    },

    // 只改变查询交易组编号
    handleTranGroup(val) {
      this.AppNum = val;
      this.tranGroupNumQuery();
    },
    // 交易组编号查询
    tranGroupNumQuery() {
      let resBody = new tranGroupNum();
      resBody.data.AppNum = this.AppNum;
      request(resBody)
        .then(response => {
          this.$set(
            this.formInline,
            "TranGroupNum",
            response.RspInfo.Result[0].TRANGROUPNUM
          );
        })
        .catch(error => {
          console.error("查询错误", error);
        });
    },
    resetForm() {
      this.$refs["formInline"].resetFields();
    },
    addTranGroup() {
      this.$refs["formInline"].validate(valid => {
        if (valid) {
          this.loading = true;
          let resBody = new tranGroup();
          resBody.data.AppNum = this.formInline.AppNum;
          resBody.data.TranGroupNum = this.formInline.TranGroupNum;
          resBody.data.TranGroupName = this.formInline.TranGroupName;
          resBody.data.TranGroupClass = this.formInline.TranGroupClass;
          resBody.data.LegalPersonNum = this.formInline.LegalPersonNum;
          resBody.data.EffeDate = this.formInline.EffeDate;
          resBody.data.ExpiDate = this.formInline.ExpiDate;
          resBody.data.TranGroupStatus = this.formInline.TranGroupStatus;
          resBody.data.SortNum = this.formInline.SortNum;
          resBody.data.OperType = "A"; // 操作类型
          request(resBody)
            .then(response => {
              if (response.SYS_HEAD.ReturnCode === "000000") {
                this.loading = false;
                this.$message({
                  type: "success",
                  message: "交易组添加成功!"
                });
                this.isShow = false;
                this.$emit("refreshBrand");
              } else {
                this.loading = false;
                this.$message({
                  type: "error",
                  message: response.SYS_HEAD.ReturnMessage
                });
              }
            })
            .catch(error => {
              this.loading = false;
              console.log(error);
            });
        } else {
          this.$message({
            type: "warning",
            message: "必输项不能为空"
          });
          return false;
        }
      });
    }
  }
};
</script>
<style lang="sass" scoped>

</style>
